//
//  OrderDetailViewController.m
//  rrqs
//
//  Created by 徐浩 on 2019/5/6.
//

#import "OrderDetailViewController.h"
#import "OrderTopTableViewCell.h"
#import "OrderGoodsTableViewCell.h"
#import "OrderGoodsTitleTableViewCell.h"
#import "OrderMessageTableViewCell.h"
#import "OrderPhoneTableViewCell.h"

static NSString *const cellID = @"OrderTopTableViewCell";
static NSString *const cellID1 = @"OrderGoodsTableViewCell";
static NSString *const cellID2 = @"OrderGoodsTitleTableViewCell";
static NSString *const cellID3 = @"OrderMessageTableViewCell";
static NSString *const cellID4 = @"OrderPhoneTableViewCell";
@interface OrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource,OrderTopTableViewCellDelegate,OrderMessageTableViewCellDelegate,OrderPhoneTableViewCellDelegate>

@property (strong, nonatomic) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *listDataArray; // 外卖数据源

@property (nonatomic, copy) NSDictionary* emptyDict;
@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单详情";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStyleGrouped)];
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 33;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self queryData];
    // Do any additional setup after loading the view.
}

#pragma mark -  DataSource
-(void)queryData{
    NSDictionary* arg = @{
                          @"ince":@"get_order_infos",
                          @"emp_id":[UserModel sharedInstanced].userId,
                          @"main_id":_waiMaiDodelListitems.order_id
                          };
    [DBNet PostURL:kHost  parameters:arg succeed:^(NSDictionary *data) {
        NSLog(@"详情data = %@",data);
        NSInteger flag = [[data objectForKey:@"status"]integerValue];
        if(flag == 1){
            NSDictionary* emptyDict = [data objectForKey:@"order"];
            
            self.emptyDict = emptyDict;
            
            self.listDataArray = [NSMutableArray arrayWithArray:emptyDict[@"goods_info"]];
            [self.tableView reloadData];
            
        }else{
            [self alertHUD:[data objectForKey:@"fail"]];
        }
    } failure:^(NSError *error) {
        [self alertHUD:@"网络异常"];
    }];
    
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1)
    {
        return self.listDataArray.count + 1;
    }
    else if (section == 2)
    {
        return 2;
    }
    else
    {
        return 1;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        if (indexPath.row == self.listDataArray.count)
        {
            OrderGoodsTitleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID2];
            if (!cell) {
                cell = [[OrderGoodsTitleTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID2];
            }
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell reloadUIWithDic:self.emptyDict];
            return cell;
        }
        OrderGoodsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID1];
        if (!cell) {
            cell = [[OrderGoodsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID1];
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell reloadUIWithDic:self.listDataArray[indexPath.row]];
        return cell;
    }
    else if (indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            OrderMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID3];
            if (!cell) {
                cell = [[OrderMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID3];
            }
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.OrderMessageTableViewCellDelegate = self;
            [cell reloadUIWithDic:self.emptyDict];
            return cell;
        }
        else
        {
            OrderPhoneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID4];
            if (!cell) {
                cell = [[OrderPhoneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID4];
            }
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.OrderPhoneTableViewCellDelegate = self;
            return cell;
        }
    }
    else
    {
        OrderTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[OrderTopTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.OrderTopTableViewCellDelegate = self;
        [cell reloadUIWithDic:self.emptyDict];
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.001;
    }else{
        return 50;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section != 0) {
        
        UIView *headerView = [[UIView alloc] init];
        headerView.backgroundColor = [UIColor whiteColor];
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(kSizeRatio(46), 0, SCREEN_WIDTH, 40)];
        [headerView addSubview:title];
        title.font = kFontNameSize(15);
        UILabel *hengLabel = [[UILabel alloc]initWithFrame:CGRectMake(kSizeRatio(46),49 , SCREEN_WIDTH - 2 * kSizeRatio(46), 1)];
        [headerView addSubview:hengLabel];
        hengLabel.backgroundColor = COLORRGB(0x999999);
        
        if (section == 1) {
            
            title.text = @"商品列表";
            
        }else{
            title.text = @"订单信息";
            
        }
        return headerView;
    }
    return nil;
}


//地图
-(void)clickMap
{
    NSLog(@"点击查看地图");
}

-(void)clickCopy
{
    [self alertHUD:@"复制成功"];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = [Config isBlankString:[NSString stringWithFormat:@"%@",self.emptyDict[@"order_number"]]] ? @"" : [NSString stringWithFormat:@"%@",self.emptyDict[@"order_number"]];
}

-(void)clickCustomer
{
    NSLog(@"点击联系顾客");
    if (![Config isBlankString:[NSString stringWithFormat:@"%@",self.emptyDict[@"user_phone"]]]) {
        [self callPhone:self.emptyDict[@"user_phone"]];
        return;
    }
    [self alertHUD:@"暂无顾客电话"];
}

-(void)clickShop
{
    NSLog(@"点击联系商家");
    if (![Config isBlankString:[NSString stringWithFormat:@"%@",self.emptyDict[@"main_phone"]]]) {
        [self callPhone:self.emptyDict[@"main_phone"]];
        return;
    }
    [self alertHUD:@"暂无商家电话"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
